/***************************************************************************
 *   Copyright (C) 2008 by Branan Riley   *
 *   branan@gmail.com   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#include "fs_support.h"

#include <physfs.h>

static QString sep;

void SetupPhysFs(char *argv0)
{
  PHYSFS_init(argv0);

  sep = QString(PHYSFS_getDirSeparator());
}

QStringList GetPk4sInDir(QString path)
{
  char **files;
  char **i;
  QString file;
  QStringList list;

  PHYSFS_addToSearchPath(path.toLocal8Bit().data(), 0);

  files = PHYSFS_enumerateFiles("");

  PHYSFS_removeFromSearchPath(path.toLocal8Bit().data());

  for(i = files; *i != 0; i++) {
    file = QString(*i);
    if(file.endsWith(QString("pk4"), Qt::CaseInsensitive)) {
      list.append(path + sep + file);
    }
  }
  PHYSFS_freeList(files);

  return list;
}

void SetupEnginePaths(QString engine_path, QString basedir, QString fs_game, QString fs_game_base)
{
  QStringList base_paths;
  QStringList fsgame_paths;
  QStringList fsgamebase_paths;
  QString base_path;
  QString fsgame_path;
  QString fsgamebase_path;

  base_path = engine_path + sep + basedir;
  base_paths = GetPk4sInDir(base_path);

  if(fs_game != basedir && fs_game != "") {
    fsgame_path = engine_path + sep + fs_game;
    fsgame_paths = GetPk4sInDir(fsgame_path);

    if(fs_game_base != basedir && fs_game_base != "") {
      fsgamebase_path = engine_path + sep + fs_game_base;
      fsgamebase_paths = GetPk4sInDir(fsgamebase_path);
    }
  }

  for(int i = 0; i < base_paths.size(); i++) {
    PHYSFS_addToSearchPath(base_paths[i].toLocal8Bit().data(), 0);
  }
  PHYSFS_addToSearchPath(base_path.toLocal8Bit().data(), 0);

  for(int i = 0; i < fsgame_paths.size(); i++) {
    PHYSFS_addToSearchPath(fsgame_paths[i].toLocal8Bit().data(), 0);
  }
  PHYSFS_addToSearchPath(fsgame_path.toLocal8Bit().data(), 0);

  for(int i = 0; i < fsgamebase_paths.size(); i++) {
    PHYSFS_addToSearchPath(fsgamebase_paths[i].toLocal8Bit().data(), 0);
  }
  PHYSFS_addToSearchPath(fsgamebase_path.toLocal8Bit().data(), 0);

}

QStringList ListFiles(QString directory, bool prepend_path)
{
  char ** files;
  char ** i;
  QString file;
  QStringList list;

  files = PHYSFS_enumerateFiles(directory.toLocal8Bit().data());

  for(i = files; *i != 0; i++) {
    file = QString(*i);
    if(prepend_path) {
      file = directory + sep + file;
    }
    list.append(file);
  }

  PHYSFS_freeList(files);

  return list;
}

QStringList ListFiles(QString directory, QString extension, bool prepend_path)
{
  char ** files;
  char ** i;
  QString file;
  QStringList list;

  files = PHYSFS_enumerateFiles(directory.toLocal8Bit().data());

  for(i = files; *i != 0; i++) {
    file = QString(*i);
    if(file.endsWith(extension, Qt::CaseInsensitive)) {
      if(prepend_path) {
        file = directory + sep + file;
      }
      list.append(file);
    }
  }

  PHYSFS_freeList(files);

  return list;
}

void recursiveFind(QStringList &list, QString dir, QString extension)
{
  char **files;
  char **i;
  QString file;

  files = PHYSFS_enumerateFiles(dir.toLocal8Bit().data());

  for(i = files; *i != 0; i++) {
    if(!dir.isEmpty()) {
      file = dir + sep + QString(*i);
    } else {
      file = QString(*i);
    }
    if(PHYSFS_isDirectory(file.toLocal8Bit().data()) != 0) {
      recursiveFind(list, file, extension);
    } else if(file.endsWith(extension, Qt::CaseInsensitive)) {
      list.append(file);
    }
  }

  PHYSFS_freeList(files);
}

QStringList FindFilesByExtension(QString extension)
{
  QStringList list;
  recursiveFind(list, QString(""), extension);
  return list;
}
